﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TimServerSDK.Core;
using TimServerSDK.Core.Enums;
using TimServerSDK.Groups.Dtos;

namespace TimServerSDK.Groups
{
    public class GroupTimServerApi : AbstractTimServerApi, IGroupTimServerApi
    {
        protected override string ServiceName { get; } = "group_open_http_svc";

        public GroupTimServerApi(IRestClientProvider restClientProvider) : base(restClientProvider)
        {

        }

        public Task<GetGroupListResultDto> GetListAsync(int limit = 10000, int next = 0, TimServerGroupTypeEnum? groupType = null)
        {
            return PostAsync<GetGroupListResultDto>("get_appid_group_list", new
            {
                Limit = limit,
                Next = next,
                GroupType = groupType
            });
        }

        public Task<CreateGroupResultDto> CreateAsync(CreateGroupInputDto input)
        {
            return PostAsync<CreateGroupResultDto>("create_group", input);
        }

        public Task<CreateGroupResultDto> CreateAsync(
            TimServerGroupTypeEnum type,
            string name,
            string ownerAccount = null,
            string groupId = null,
            string introduction = null,
            string notification = null,
            string faceUrl = null,
            int? maxMemberCount = null,
            TimServerApplyJoinEnum applyJoinOption = TimServerApplyJoinEnum.NeedPermission,
            CreateGroupInputDto.Appdefineddata[] appDefinedData = null,
            CreateGroupInputDto.Memberlist[] memberList = null)
        {
            return CreateAsync(new CreateGroupInputDto()
            {
                Type = type,
                Name = name,
                Owner_Account = ownerAccount,
                GroupId = groupId,
                Introduction = introduction,
                Notification = notification,
                FaceUrl = faceUrl,
                MaxMemberCount = maxMemberCount,
                ApplyJoinOption = applyJoinOption,
                AppDefinedData = appDefinedData,
                MemberList = memberList
            });
        }

        public Task<TimServerResult> ModifyBaseInfoAsync(
            string groupId,
            string name = null,
            string introduction = null,
            string notification = null,
            string faceUrl = null,
            int? maxMemberNum = null,
            string applyJoinOption = null,
            string muteAllMember = null,
            Dictionary<string, string> appDefinedData = null)
        {
            return PostAsync<TimServerResult>("modify_group_base_info", new
            {
                GroupId = groupId,
                Name = name,
                introduction = introduction,
                Notification = notification,
                FaceUrl = faceUrl,
                MaxMemberNum = maxMemberNum,
                ApplyJoinOption = applyJoinOption,
                MuteAllMember = muteAllMember,
                AppDefinedData = appDefinedData == null ? null : appDefinedData.Select(item =>
                {
                    return new
                    {
                        Key = item.Key,
                        Value = item.Value
                    };
                }).ToArray()
            });
        }

        public Task<AddGroupMemberResultDto> AddMemberAsync(string groupId, string[] accounts, bool silence = false)
        {
            return PostAsync<AddGroupMemberResultDto>("add_group_member", new
            {
                GroupId = groupId,
                Silence = silence ? 1 : 0,
                MemberList = accounts.Select(account =>
                {
                    return new
                    {
                        Member_Account = account
                    };
                }).ToList()
            });
        }

        public Task<TimServerResult> DeleteMemberAsync(string groupId, string[] accounts, bool silence = false, string reason = null)
        {
            return PostAsync<TimServerResult>("delete_group_member", new
            {
                GroupId = groupId,
                Silence = silence ? 1 : 0,
                Reason = reason,
                MemberToDel_Account = accounts
            });
        }

        public Task<TimServerResult> DestroyAsync(string groupId)
        {
            return PostAsync<TimServerResult>("destroy_group", new
            {
                GroupId = groupId
            });
        }
    }
}
